﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace WXMarketing
{
    public class WXStrAnalyser
    {
        private static DataTable MakeAVDTable()
        {
            // Create a new DataTable titled 'Names.'
            DataTable avdTable = new DataTable("AVDTable");

            // Add three column objects to the table.
            DataColumn idColumn = new DataColumn();
            idColumn.DataType = System.Type.GetType("System.Int32");
            idColumn.ColumnName = "ID";
            idColumn.AutoIncrement = true;
            avdTable.Columns.Add(idColumn);

            DataColumn nameColumn = new DataColumn();
            nameColumn.DataType = System.Type.GetType("System.String");
            nameColumn.ColumnName = "Name";
            avdTable.Columns.Add(nameColumn);

            DataColumn targetColumn = new DataColumn();
            targetColumn.DataType = System.Type.GetType("System.String");
            targetColumn.ColumnName = "Target";
            avdTable.Columns.Add(targetColumn);

            DataColumn pathColumn = new DataColumn();
            pathColumn.DataType = System.Type.GetType("System.String");
            pathColumn.ColumnName = "Path";
            avdTable.Columns.Add(pathColumn);

            // Create an array for DataColumn objects.
            DataColumn[] keys = new DataColumn[1];
            keys[0] = idColumn;
            avdTable.PrimaryKey = keys;

            // Return the new DataTable.
            return avdTable;
        }

        public static DataTable AnalyseAVDDevice(string _str)
        {
            int indexHeader = 0;
            int indexTail = 0;
            int rowCount = 0;
            string tmpString = "";
            string[] strpair;
            bool hasName = false;
            bool hasPath = false;
            bool hasTarget = false;
            bool hasABI = false;
            bool hasSkin = false;
            bool hasSdcard = false;

             string name = "";
             string path = "";
             string target = "";
            DataTable retTable = MakeAVDTable();
            indexTail = _str.IndexOf('\n');
            if (_str.Substring(indexHeader, indexTail - indexHeader) != "Available Android Virtual Devices:")
            {
                return null;
            }
            while (indexTail < _str.Length - 1)
            {
                
                indexHeader = indexTail + 1;
                indexTail = _str.IndexOf('\n',indexHeader);
                tmpString = _str.Substring(indexHeader, indexTail - indexHeader);
                if (tmpString == "---------")
                {
                    if (hasName && hasPath && hasTarget && hasABI && hasSdcard && hasSkin)
                    {
                        DataRow dr = retTable.NewRow();
                        dr["Name"] = name;
                        dr["Path"] = path;
                        dr["Target"] = target;
                        retTable.Rows.Add(dr);
                        hasName = false;
                        hasPath = false;
                        hasABI = false;
                        hasSdcard = false;
                        hasSkin = false;
                        hasTarget = false;
                    }
                    continue;
                }
                strpair = tmpString.Split(':');
                switch (strpair.Length)
                {
                    case 3:
                        if (strpair[0].Trim().ToLower() != "path")
                            continue;
                        break;
                    case 2:
                        break;
                    default:
                        continue;
                        break;
                }

                switch (strpair[0].Trim().ToLower())
                {
                    case "name":
                        name = strpair[1].Trim();
                        hasName = true;
                        break;
                    case "path":
                        path = strpair[1].Trim() + strpair[2];
                        hasPath = true;
                        break;
                    case "target":
                        target = strpair[1].Trim();
                        hasTarget = true;
                        break;
                    case "abi":
                        if (strpair[1].Trim().ToLower() == "armeabi")
                        hasABI = true;
                        break;
                    case "skin":
                        if (strpair[1].Trim().ToLower() == "320x480")
                            hasSkin = true;
                        break;
                    case "sdcard":
                        if (strpair[1].Trim().ToLower() == "256m")
                        hasSdcard = true;
                        break;
                    default:
                        break;
                }
            }

            return retTable;
        }
    }
}
